Dim args
Dim retVal
Dim Command

Public Const mqlUrl 	= "http://cwiprod.corp.halliburton.com/cwi/just_mql.jsp?user=tmp&ignorePasswd=true&input="
Public Const queryBase 	= "print bus Rta R00000[RTANUM] '' select [SELECT] dump |"
Public Const modBase 	= "mod bus Rta R00000[RTANUM] '' '[ATTRIB]' '[NEWVAL]'"


If WScript.Arguments.Count < 2 Then
	WScript.Echo "Invalid Arguments"
	WScript.Quit(0)
End If


Set args = WScript.Arguments

Command = UCase(args(1))

Select Case Command
	Case "DESC"
		retVal = GetDescription(args(0))
	Case "ID"
		retVal = GetID(args(0))
	Case "COM"
		retVal = GetComments(args(0))
	Case "CUST"
		retVal = GetAttribute(args(0))
	Case "ATTRIB"
		retVal = GetAttribute(args(0), args(2))
	Case "EXISTS"
		retVal = Exists(args(0))
	Case "MODDESC"
		retVal = ModifyDescription(args(0), args(2))
	Case "MODCUST"
		retVal = ModifyCustomer(args(0), args(2))
	Case "MODCOM"	
		retVal = ModifyComments(args(0), args(2))
	Case "MODATTRIB"
		retVal = ModifyAttribute(args(0), args(2), args(3))
	Case "MOD"
		Call CreateModifyRta(args(0))
	Case Else
		WScript.Echo "Invalid Arguments"
		WScript.Quit(1)
End Select


WScript.Echo Trim(retVal)


''' -------------------------------------------------------------------------------------
''' GetSelect
''' Performs the given temp query and returns the fourth value
''' in the pipe separated result
''' -------------------------------------------------------------------------------------
Function GetAttribute(RtaNum, Attribute)
	Dim qry
	Dim response
	Dim tmpArray
	qry = Replace(Replace(queryBase, "[SELECT]", "attribute[" & attribute & "]"), "[RTANUM]", RtaNum)
	GetAttribute = GetResponse(qry)
End Function




''' --------------------------------------------------------------------
''' GetID
''' Returns the Rta's ID
''' --------------------------------------------------------------------
Function GetID(RtaNum)
	Dim qry

	qry = Replace(Replace(queryBase, "[SELECT]", "id"), "[RTANUM]", RtaNum)
	GetID = GetResponse(qry)
End Function




''' --------------------------------------------------------------------
''' GetComments
''' Returns the Rta's Comments
''' --------------------------------------------------------------------
Function GetComments(RtaNum)
	Dim qry
	qry = Replace(Replace(queryBase, "[SELECT]", "attribute[Comments]"), "[RTANUM]", RtaNum)
	GetComments = GetResponse(qry)
End Function



''' --------------------------------------------------------------------
''' GetDescription
''' Returns the Rta's description
''' --------------------------------------------------------------------
Function GetDescription(rtaNum)
	Dim qry

	qry = Replace(Replace(queryBase, "[SELECT]", "description"), "[RTANUM]", RtaNum)
	GetDescription = GetResponse(qry)
End Function


''' --------------------------------------------------------------------
''' Exists
''' Returns True/False based on whether the RTA exists
''' --------------------------------------------------------------------
Function Exists(RtaNum)
	Dim qry
	qry = Replace(Replace(queryBase, "[SELECT]", "exists"), "[RTANUM]", RtaNum)
	Exists = GetResponse(qry)
End Function



''' --------------------------------------------------------------------
''' ModifyDescription
''' 
''' --------------------------------------------------------------------
Function ModifyDescription(RtaNum, NewVal)
	Dim qry
	Dim retVal
	qry = Replace(Replace(Replace(modBase, "[RTANUM]", RtaNum), "[ATTRIB]", "description"), "[NEWVAL]", NewVal)
	retVal = GetResponse(qry)
	If InStr(1, Trim(retVal), "OK:") > 0 Then
		ModifyDescription = "Success"
	Else
		ModifyDescription = retVal
	End If
End Function





''' --------------------------------------------------------------------
''' ModifyCustomer
''' 
''' --------------------------------------------------------------------
Function ModifyCustomer(RtaNum, NewVal)
	Dim qry
	Dim retVal
	qry = Replace(Replace(Replace(modBase, "[RTANUM]", RtaNum), "[ATTRIB]", "Customer Name"), "[NEWVAL]", NewVal)
	retVal = GetResponse(qry)
	If InStr(1, Trim(retVal), "OK:") > 0 Then
		ModifyCustomer = "Success"
	Else
		ModifyCustomer = retVal
	End If
End Function




''' --------------------------------------------------------------------
''' ModifyComments
''' 
''' --------------------------------------------------------------------
Function ModifyComments(RtaNum, NewVal)
	Dim qry
	Dim retVal
	qry = Replace(Replace(Replace(modBase, "[RTANUM]", RtaNum), "[ATTRIB]", "Comments"), "[NEWVAL]", NewVal)
	retVal = GetResponse(qry)
	If InStr(1, Trim(retVal), "OK:") > 0 Then
		ModifyComments = "Success"
	Else
		ModifyComments = retVal
	End If
End Function





''' --------------------------------------------------------------------
''' CreateModifyRta
''' Opens the RTA in CWI in the Create/Modify RTA view
''' --------------------------------------------------------------------
Sub CreateModifyRta(RtaNum)
	Dim id
	Dim ie
	id = GetID(RtaNum)
	Set IE = CreateObject("InternetExplorer.Application")
	IE.Visible = 1
	IE.Navigate "http://cwiprod.corp.halliburton.com/cwi/CreateModifyRta.jsp?id=" & id
	Set ie = Nothing
End Sub




''' --------------------------------------------------------------------
''' ModifyAttribute
''' 
''' --------------------------------------------------------------------
Function ModifyAttribute(RtaNum, Attrib, NewVal)
	Dim qry
	Dim retVal
	qry = Replace(Replace(Replace(modBase, "[RTANUM]", RtaNum), "[ATTRIB]", attrib), "[NEWVAL]", NewVal)
	retVal = GetResponse(qry)
	If InStr(1, Trim(retVal), "OK:") > 0 Then
		ModifyAttribute = "Success"
	Else
		ModifyAttribute = retVal
	End If
End Function





''' -------------------------------------------------------------------------------------
''' GetResponse
''' Returns the result of the MQL query as a string
''' -------------------------------------------------------------------------------------
Function GetResponse(queryStr)
	Dim IE	
	Dim result	
	Set IE = CreateObject("MSXML2.XMLHTTP.6.0")
	IE.open "POST", mqlUrl & queryStr
	IE.send
	result = IE.responseText
	Set IE = Nothing
	GetResponse = Trim(result)
End Function


